package com.czl.module_service.viewmodel

import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AssetHandleSortInuseViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var parentCompanyId: Int = 0
    var currentPage = 1
    var pageSize = 10
    val uc = UiChangeEvent()
    var sortIdInfoList: HashMap<String, Int> = hashMapOf()

    var sortList: MutableList<AssetHandleSortBean>? = arrayListOf()
//    var productList: List<AssetHandleSortBean>? = arrayListOf()

    class UiChangeEvent {
        val loadSortEvent: SingleLiveEvent<MutableList<AssetHandleSortBean>> = SingleLiveEvent()
        val loadProductEvent: SingleLiveEvent<MutableList<AssetHandleSortBean>> = SingleLiveEvent()
        val loadSubmitEvent: SingleLiveEvent<MutableList<AssetHandleSortBean>> = SingleLiveEvent()
    }

    val selectedSubmit: BindingCommand<Void> = BindingCommand(BindingAction {
        var selectedSortList: MutableList<AssetHandleSortBean>? = arrayListOf()
        for (bean in sortList!!) {
            if (bean.manageMode == 1) {
                if (bean.selected != null && bean.selected!!) {
                    bean.handleSelectType = "inuse"
                    selectedSortList?.add(bean)
                }
            } else {
                if (bean.assetCountStr != null && !bean.assetCountStr!!.isEmpty()) {
                    bean.assetCount =  bean.assetCountStr!!.toInt()
                    if (bean.assetCount!! > bean.useNum!!) {
                        showNormalToast("物品（" + bean.sortName + "）处置数量大于待处置数量")
                        return@BindingAction
                    }
                    bean.handleSelectType = "inuse"
                    selectedSortList?.add(bean)
                }
            }
        }
        uc.loadSubmitEvent.postValue(selectedSortList)
    })

    /*val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        this.getPageList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        this.getPageList()
    })*/

    fun getProcessSortUseInfo(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        titleName: String? = null
    ) {

        sortList!!.clear()
        model.mTengYunHttpDataSource.getProcessSortUseInfo(
            GsonUtils.toJson(
                mapOf(
                    "userId" to userId,
                    "propCompanyId" to companyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<MutableList<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<MutableList<AssetHandleSortBean>>) {
                    if (t.state == 0) {
                        sortList?.addAll(t.data!!)
                        for (bean in sortList!!){
                            var num = sortIdInfoList.get("0##"+bean.sortId)
                            if (num != null && num > 0){
                                bean.assetCount = num
                            }
                        }
//                    uc.loadSortEvent.postValue(sortList)
//                    dismissLoading()
                        getProcessProUseInfo()
                        dismissLoading()
                    } else {
                        dismissLoading()
                        showErrorToast(t.msg)
                    }

                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getProcessProUseInfo(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        titleName: String? = null
    ) {
        var isAssets: Int = 0
        var applyId: Int? = null
        model.mTengYunHttpDataSource.getProcessProUseInfo(
            GsonUtils.toJson(
                mapOf(
                    "isAssets" to isAssets,
                    "applyId" to applyId,
                    "userId" to userId,
                    "propCompanyId" to companyId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<MutableList<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<MutableList<AssetHandleSortBean>>) {
                    if (t.state == 0) {
                        sortList?.addAll(t.data!!)
//                    uc.loadProductEvent.postValue(t.data)
                        uc.loadSortEvent.postValue(sortList)
                        dismissLoading()
                    } else {
                        dismissLoading()
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}